home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / mrcry204.zip / X0.C < prev    next >
Text File  |  1991-03-04  |  3KB  |  130 lines

  1.  
  2. /* X0.c
  3.  
  4. This is the startup module for external functions.
  5.  
  6. There is an alternate startup module in this file for external
  7. function which do not require DS=SS.  The math library, ld.lib,
  8. which has been provided, does not require DS=SS.  Programs built
  9. with this save some disk space.
  10.  
  11. Options.
  12.  
  13. You must set Opt=1 or Opt=2.
  14.  
  15. 1. External function sets up its own stack segment.  This allows
  16. you to guarantee enough stack space, but more importantly, it allows
  17. DS=SS which is what the compiler expects in tiny model.  Unfortunately,
  18. however, the floating point emulator doesn't work in a foreign stack
  19. segment.  Do not use this option unless you are using a math coprocessor.
  20.  
  21. 2. External function uses Mercury's stack segment.  This is more efficient,
  22. but requires you to write your function in tiny model without using DS=SS,
  23. which Turbo C tiny model normally assumes.  Most simple functions in tiny
  24. model don't actually make use of DS=SS, but this option is dangerous unless
  25. you are familiar with the way Turbo C generates code.
  26.  
  27. tcc -c -mt -w -ox01 -DOpt=1 -Z -O x0
  28. tcc -c -mt -w -ox02 -DOpt=2 -Z -O x0
  29.  
  30. x01.obj uses stack swap
  31. x02.obj uses Mercury stack
  32.  
  33. See the examples, GCD.C and ASINH.C, for instructions on how to link.
  34. Using Option 1 requires linking with F87.OBJ.
  35. */
  36.  
  37.  
  38. #pragma inline
  39.  
  40. /* TC is not too happy about this, but it does the right thing. */
  41. #pragma warn -asm
  42. asm DGROUP    group    _TEXT,_DATA,_BSS
  43. #pragma warn .asm
  44.  
  45. pascal xmain(double far *);
  46.  
  47. /*
  48. To handle tiny model properly, must have CS=DS=SS.
  49. Simple programs, eg not passing addresses of local variables, don't need SS.
  50.  
  51. */
  52.  
  53. #ifndef    Opt
  54. #error Must define Opt=1 or Opt=2.
  55. #endif
  56.  
  57. #if    Opt == 1
  58. #define    SwapSS    1
  59. #endif
  60.  
  61. #if    Opt == 2
  62. #define    SwapSS    0
  63. #endif
  64.  
  65. #if    !SwapSS
  66.  
  67. /* Opt = 2 */
  68.  
  69. static void far pascal start(double *a)
  70. {
  71.  
  72.     _ES = _SI = _DS;
  73.     _DS = _CS;
  74.  
  75.     xmain((double _es *) a);
  76.  
  77.     _DS = _SI;
  78.     return;
  79. }
  80.  
  81. #else
  82.  
  83. /* Opt = 1 */
  84.  
  85. /* make external, so user can set it? */
  86. #define StackWords    1000
  87. int stack[StackWords] = { 0 };
  88.  
  89. /*
  90. Must preserve bp, si, di, ds, ss.
  91. Called with ds = ss.
  92. */
  93.  
  94. static void far pascal start(double *a)
  95. {
  96.     _BX = (unsigned int) a;
  97. asm    mov    cx, sp
  98. asm    mov    dx, ss
  99.  
  100.     _AX = (unsigned int) (&stack[StackWords]);
  101.  
  102. /* switch to local stack */
  103. asm    push    cs
  104. asm    pop    ss
  105. asm    mov    sp, ax
  106.  
  107. /* save prior ss, sp */
  108. asm    push    dx
  109. asm    push    cx
  110.  
  111. /* set ds=cs */
  112. asm    push    cs
  113. asm    pop    ds
  114.  
  115. /* call xmain */
  116. asm    mov    es, dx
  117.     xmain((double _es *) _BX);
  118.  
  119. /* restore ss, sp, ds=ss */
  120. asm    pop    cx
  121. asm    pop    dx
  122. asm    mov    ss, dx
  123. asm    mov    sp, cx
  124. asm    mov    ds, dx
  125.  
  126.     return;
  127. }
  128.  
  129. #endif
  130.